<?php
/**
 * Vebba PHP Framework & Website Builder
 *
 * <pre>
 * File containing functionalities for miscellaneous utilities
 * </pre>
 *
 * @package   classes.core.static
 * @version   0.7.1511 alpha
 * @since     2012
 * @author    Paul Dinh
 * @copyright Devyra Internacionalnaya
 */
 
/**
 * Class contains miscellaneous utils
 *
 * @package classes.core.static
 * @version 0.7.1511 alpha
 * @since   2012
 * @author  Paul Dinh
 */
class helper {

  /**
   * Default instance level constructor
   */
  public function __construct() {
    //
  }
  
  /**
   * Some initialisation
   */
  public static function initialise() {
    //
  }
  
  /**
   * Check if an email address exists
   *
   * @param  string $Email The email address to check
   * @return bool          'true' if the email address exists
   */
  public static function email_exists($Email) {
    if (strpos($Email,"@")===false)
      return false;
      
    $p = strpos($Email,"@");
    $Domain = substr($Email,$p+1);
    
    $Mxes = array();
    if (dns_get_mx($Domain,$Mxes)==false)
      return false;
    else
      return true;
  }  
  
  /**
   * Trim trailing & tailing characters, also extra spaces in between
   *
   * @param  string $Text The text to trim
   * @return string       The trimmed text
   */
  public static function trim_neat($Text) {
    $Text = trim($Text);
    while (strpos($Text,chr(32).chr(32))!==false)
      $Text = str_replace(chr(32).chr(32),chr(32),$Text);
    return $Text;
  }
  
  /**
   * Dump object to file 'dump.txt'
   *
   * @param object $Object The object to dump
   */
  public static function dump($Object) {
    ob_start();
    var_dump($Object);
    $Text = ob_get_clean();
    file_put_contents("dump.txt",$Text);
  }
  
  /**
   * Get the list of available langs as set in config.xml
   * 
   * @return array The associative array of lang abbreviations to lang names   
   */
  public static function get_lang_list() {
    $Configs = system::$Configs;
    $Langs   = $Configs["languages"]["language"];
    $List    = array();
    
    foreach ($Langs as $Lang) {
      $Attrs               = $Lang["@attributes"];
      $Abbreviation        = $Attrs["abbreviation"];
      $Lang_Name           = $Attrs["name"];
      $List[$Abbreviation] = $Lang_Name;
    }
    
    return $List;
  }
  
  /**
   * Add actions & params to current page URL (no protocol, no path)
   *
   * @param  array  $Actions The additional action & params
   * @return string          The resulting internal URL
   */
  public static function add_params($Addin_Params) {
    $Module  = request::$Module;
    $Page    = request::$Page;
    $Actions = request::$Actions;
    $Params  = request::$Params;
    
    //add actions & params
    foreach ($Addin_Params as $Action=>$Values) {
      if (!in_array($Action,$Actions)) {
        array_push($Actions,$Action);
        $Params[$Action] = array();
      }
      
      foreach ($Values as $Key=>$Value)
        $Params[$Action][$Key] = $Value;
    }//foreach action
    
    //build internal url
    $Url = $Module."/".$Page;
    foreach ($Params as $Action=>$Values) {
      if ($Action!="index")
        $Url .= "/".$Action;
      foreach ($Values as $Key=>$Value)
        $Url .= "/".$Key."-".$Value;
    }
    
    return $Url;
  }
  
  /**
   * Get script name 
   *
   * <pre>
   * This is the path & script file name after server name,
   * combines with protocol & server name will be the URL without query string.
   * If already URL-rewritten, this function returns the actual script file.
   * This is actually the path to index.php at framework root.
   * </pre>
   *
   * @return string The script path & script file name
   */
  public static function get_script_name() {
  
    //PHP_SELF has the same value but not popular
    //REQUEST_URI has the query string included
    return strtolower($_SERVER["SCRIPT_NAME"]);
  }
  
  /**
   * Get the path (after domain & before script file name) to requested script
   *
   * <pre>
   * This path doesn't contain slashes at beginning & end;
   * this function fails if being called from somewhere else other than root
   * directory of the framework.
   * </pre>
   *
   * @return string The path after domain & before file name
   */
  public static function get_script_path() {
    $Script_Name = self::get_script_name();
    $Last_Slash  = strrpos($Script_Name,"/");
    
    if ($Last_Slash===0)
      return "";
    else
      return substr($Script_Name,1,$Last_Slash-1);
  }
}

//END-OF-FILE
